package com.xiuhong.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mysql.cj.xdevapi.JsonArray;
import com.xiuhong.annotation.LoginUser;
import com.xiuhong.cache.J2CacheUtils;
import com.xiuhong.entity.BuyGoodsVo;
import com.xiuhong.entity.CartVo;
import com.xiuhong.entity.UserVo;
import com.xiuhong.util.ApiBaseAction;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * zxh
 *
 * @gitee https://gitee.com/fuyang_lipengjun/platform
 */
@Api(tags = "商品购买")
@RestController
@RequestMapping("/api/buy")
public class ApiBuyController extends ApiBaseAction {

    @ApiOperation(value = "商品添加")
    @PostMapping("/add")
    public Object addBuy(@LoginUser UserVo loginUser) {
        List<CartVo> checkedGoodsList = new ArrayList<>();
        BigDecimal goodsTotalPrice;
        JSONObject jsonParam = getJsonRequest();
        Integer goodsId = jsonParam.getInteger("goodsId");
        Integer productId = jsonParam.getInteger("productId");
        Integer number = jsonParam.getInteger("number");
        BuyGoodsVo goodsVo = new BuyGoodsVo();
        goodsVo.setGoodsId(goodsId);
        goodsVo.setProductId(productId);
        goodsVo.setNumber(number);
        J2CacheUtils.put(J2CacheUtils.SHOP_CACHE_NAME, "goods" + getUserId() + "", goodsVo);


        return toResponsMsgSuccess("添加成功");
    }

    @ApiOperation(value = "商品添加")
    @PostMapping("/addtoCache")
    public Object addtoCache(@LoginUser UserVo loginUser) {
        List<BuyGoodsVo> checkedGoodsList = new ArrayList<>();
        BigDecimal goodsTotalPrice;
        JSONObject jsonParam = getJsonRequest();
        String goodsIds = jsonParam.getString("goodsId");
        String numbers  = jsonParam.getString("allNum");
        String[] goodsArray = goodsIds.split(",");
        String[] numbersArray = numbers.split(",");
        for (int i = 0; i <goodsArray.length ; i++)
        {
            BuyGoodsVo goodsVo = new BuyGoodsVo();
            goodsVo.setGoodsId( Integer.valueOf(goodsArray[i]));
            goodsVo.setNumber(Integer.valueOf(numbersArray[i]));
            checkedGoodsList.add(goodsVo);
        }

        JSONArray arr = jsonParam.getJSONArray("arr");


        J2CacheUtils.put(J2CacheUtils.SHOP_CACHE_NAME, "goods" + getUserId() + "", checkedGoodsList);



        return toResponsMsgSuccess("添加成功");
    }

}
